package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.cargpsmanage.entity.CarGpsApply;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.CarGpsApplyService;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.GpsManageService;
import com.ruicar.afs.cloud.afscase.loanapprove.condition.LoanApproveCondition;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.GpsStatusEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.loan.gpsdto.ApplyGpsInfoDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

/**
 * @Description GPS二次派单MQ消息接收处理类
 * @Author yk.Li
 * @Date 2020/08/10
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit",name="enable")
public class GpsApplyProcessor implements AfsMqBizProcessor<ApplyGpsInfoDto> {

    private final CarGpsApplyService carGpsApplyService;
    private final GpsManageService gpsManageService;


    @Override
    public boolean processMessage(ApplyGpsInfoDto applyGpsInfoDto) throws Exception {
        try {
            log.info(">>>>>>>>>>GPS二次派单信息同步 START>>>>>>>>>>>>", applyGpsInfoDto);
            this.parseCarGpsApply(applyGpsInfoDto);
            log.info(">>>>>>>>>>GPS二次派单信息同步 END>>>>>>>>>>>>", applyGpsInfoDto);
        } catch (Exception e) {
            log.error("GPS二次派单信息同步失败", applyGpsInfoDto, e.getMessage());
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_GPS_AGAIN_DISTRIBUTE;
    }

    /**
     * @Description 解析GPS申请信息
     * @Author yk.Li
     * @Date 2020/08/10
     */
    @Transactional
    public void parseCarGpsApply(ApplyGpsInfoDto carGpsApplyDto) {
        Assert.isTrue(!ObjectUtils.isEmpty(carGpsApplyDto), "二次派单信息处理失败,未接收到的信息二次派单信息...");
        CarGpsApply carGpsApply = carGpsApplyService.getOne(Wrappers.<CarGpsApply>query().lambda()
                .eq(StringUtils.isNotBlank(carGpsApplyDto.getContractNo()), CarGpsApply::getContractNo, carGpsApplyDto.getContractNo())
                .eq(null != carGpsApplyDto.getCarId(), CarGpsApply::getCarId, carGpsApplyDto.getCarId()));
        Assert.isTrue(null != carGpsApply, "合同号:【" + carGpsApplyDto.getContractNo() + "】，carId:【" + carGpsApplyDto.getCarId() + "】派单信息不存在.");

        CarGpsApply carGpsApplySecond = new CarGpsApply();
        BeanUtils.copyProperties(carGpsApply, carGpsApplySecond, new String[]{"id"});
        //删除历史派单数据
        carGpsApplyService.removeById(carGpsApply);

        //保存二次派单数据
        carGpsApplySecond.setApplyStatus(AfsEnumUtil.key(GpsStatusEnum.SEND_TASK));
        carGpsApplyService.save(carGpsApplySecond);

        //二次派单提交
        LoanApproveCondition condition = new LoanApproveCondition();
        condition.setApplyNo(carGpsApplyDto.getApplyNo());
        condition.setContractNo(carGpsApplyDto.getContractNo());
        condition.setAutoFlag(Boolean.TRUE);
        //gpsManageService.gpsSecondAssignmentSubmit(condition);
    }
}
